package com.guigu.liuyunan.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.liuyunan.mapper.OrderMapper;
import com.guigu.liuyunan.mapper.OrdersettingMapper;
import com.guigu.liuyunan.pojo.LYNPageVo;
import com.guigu.liuyunan.pojo.Order;
import com.guigu.liuyunan.pojo.Ordersetting;
import com.guigu.liuyunan.service.OrederService;
import com.guigu.tankai.mapper.MemberMapper;
import com.guigu.tankai.mapper.MemberexaminationMapper;
import com.guigu.tankai.pojo.Member;
import com.guigu.tankai.pojo.Memberexamination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class OrederServiceImpl implements OrederService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    MemberexaminationMapper mm;

    @Autowired
    OrdersettingMapper os;

    @Autowired
    MemberMapper memberMapper;

    @Override
    public PageInfo OrderInfo(LYNPageVo LYNPageVo) {
        PageHelper.startPage(LYNPageVo.getPage(), LYNPageVo.getNum());
        List<Order> byPagination = orderMapper.OrderInfo(LYNPageVo);
        return new PageInfo(byPagination);
    }

    @Override
    public int orderUp(Order order) {
        // 预约状态
        Member member = new Member();
        member.setId(order.getMembeid());
        member.setOrderzt(2);
        memberMapper.zt(member);
        return orderMapper.orderUp(order);
    }

    @Override
    public int orderQX(Order order) {
        // 预约状态
        Member member = new Member();
        member.setId(order.getMembeid());
        member.setOrderzt(0);
        memberMapper.zt(member);
        return orderMapper.orderQX(order);
    }


    @Override
    public int orderAdd(Order order) {
        Order order1 = orderMapper.OrderDate(order);
        if (order1==null){

            Ordersetting ordersetting = os.selectByPrimaryKey(order.getOrderdate());

            ordersetting.setReservations(ordersetting.getReservations()+1);
            os.updateByPrimaryKey(ordersetting);

            List<Memberexamination> list=new ArrayList<>();
            Integer[] setmealList = order.getSetmealList();
            for (int i = 0; i < setmealList.length; i++) {
                Memberexamination mem = new Memberexamination();
                mem.setSetmealid(setmealList[i]);
                mem.setMemberid(order.getMembeid());
                mem.setExamDate(order.getOrderdate());
                list.add(mem);
            }
            mm.MemberexaminationAdd(list);

            // 预约状态
            Member member = new Member();
            member.setId(order.getMembeid());
            member.setOrderzt(1);
            memberMapper.zt(member);
            return orderMapper.insert(order);
        }else{
            return 0;
        }
    }

    @Override
    public List<Order> tkfont() {
        return orderMapper.tkfont();
    }
    @Override
    public List<Order> tkcount() {
        return orderMapper.tkcount();
    }

    @Override
    public List<Order> tkcounto() {
        return orderMapper.tkcounto();
    }

    @Override
    public List<Order> tanhytjcha(String startDate, String endDate) {
        List<Order> tanhytjcha = orderMapper.tanyyrscha(startDate, endDate);
        return tanhytjcha;
    }

    @Override
    public List<Order> tkhytjcha(String startDate, String endDate) {
        return orderMapper.tkbgsccha(startDate, endDate);
    }

    @Override
    public Order cha(Integer mid) {
        return orderMapper.cha(mid);
    }
}
